package chapter10;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @create 2018-03-22 上午10:19
 */

interface Fruit {
    public void eat();
}

class Orange implements Fruit {

    @Override
    public void eat() {
        System.out.println("this is orange");
    }
}

class Apple implements Fruit {

    @Override
    public void eat() {
        System.out.println("this is apple");
    }
}

class Factory {
    public static Fruit getInstance(String name) {
        Fruit fruit = null;
        try {
            fruit = (Fruit) Class.forName(name).newInstance();
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return fruit;
    }
}
public class ReflectFactoryTest {

    public static void main(String[] args) {
        Fruit a = Factory.getInstance("Apple");
        Fruit b = Factory.getInstance("Orange");
        a.eat();
        b.eat();
    }

}
